import pygame
import sys
import os
import time
from utils.config import SCREEN_WIDTH, SCREEN_HEIGHT, BG_IMAGE, FONT_NAME

class WelcomeInterface:
    def __init__(self, bg_image_filename=None):
        pygame.init()
        self.width, self.height = SCREEN_WIDTH, SCREEN_HEIGHT
        self.screen = pygame.display.set_mode((self.width, self.height))
        pygame.display.set_caption("Snake Game")
        self.font = pygame.font.SysFont('Arial', 64)
        self.small_font = pygame.font.SysFont('Arial', 36)
        self.menu = [
            "1. Start Game",
            "2. Show High Score",
            "3. Game Description",
            "4. Exit"
        ]
        self.selected = 0
        self.bg_image = None
        if bg_image_filename:
            assets_path = os.path.join(os.path.dirname(__file__), '..', 'assets')
            image_path = os.path.join(assets_path, bg_image_filename)
            if os.path.exists(image_path):
                self.bg_image = pygame.image.load(image_path)
                self.bg_image = pygame.transform.scale(self.bg_image, (self.width, self.height))
                
    def get_window_position():
        pos = os.environ.get('SDL_VIDEO_WINDOW_POS')
        if pos:
            x, y = pos.split(',')
            return int(x), int(y)
        try:
            import ctypes
            user32 = ctypes.windll.user32
            hwnd = pygame.display.get_wm_info()['window']
            rect = ctypes.wintypes.RECT()
            ctypes.windll.user32.GetWindowRect(hwnd, ctypes.byref(rect))
            return rect.left, rect.top
        except Exception:
            return (100, 100)
                
    def show(self):
        running = True
        menu_rects = []
        while running:
            if self.bg_image:
                self.screen.blit(self.bg_image, (0, 0))
            else:
                self.screen.fill((0, 0, 0))
            title_surface = self.font.render("Snake Game", True, (0, 255, 0))
            title_rect = title_surface.get_rect(center=(self.width // 2, 150))
            self.screen.blit(title_surface, title_rect)
            menu_rects.clear()
            for idx, item in enumerate(self.menu):
                color = (255, 255, 0) if idx == self.selected else (255, 255, 255)
                menu_surface = self.small_font.render(item, True, color)
                menu_rect = menu_surface.get_rect(center=(self.width // 2, 300 + idx * 60))
                self.screen.blit(menu_surface, menu_rect)
                menu_rects.append(menu_rect)
            pygame.display.flip()

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_DOWN:
                        self.selected = (self.selected + 1) % len(self.menu)
                    elif event.key == pygame.K_UP:
                        self.selected = (self.selected - 1) % len(self.menu)
                    elif event.key in [pygame.K_1, pygame.K_KP1]:
                        pygame.time.delay(200)
                        return 1
                    elif event.key in [pygame.K_2, pygame.K_KP2]:
                        pygame.time.delay(200)
                        return 2
                    elif event.key in [pygame.K_3, pygame.K_KP3]:
                        pygame.time.delay(200)
                        return 3
                    elif event.key in [pygame.K_4, pygame.K_KP4]:
                        pygame.time.delay(200)
                        return 4
                    elif event.key == pygame.K_RETURN or event.key == pygame.K_KP_ENTER:
                        pygame.time.delay(200)
                        return self.selected + 1
                elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                    mouse_pos = pygame.mouse.get_pos()
                    for idx, rect in enumerate(menu_rects):
                        if rect.collidepoint(mouse_pos):
                            pygame.time.delay(200)
                            return idx + 1
